from flask import request, Blueprint, send_file
import os
from config.config import serverPath, domain

updata = Blueprint('updata', __name__)


@updata.route('/userupimage', methods=['POST'])
def upload_user_file():
    if 'file' not in request.files:
        return 'No file selected'
    file = request.files['file']
    if file.filename == '':
        return 'No file selected'
    if file:
        print(file.filename)
        file.save(os.path.join(serverPath, 'usernavimage/', file.filename))
        return domain + "/usernavimage/" + file.filename


@updata.route('/usernavimage/<filename>')
def get_user_image(filename):
    # 构建图片文件的完整路径
    image_path = os.path.join(serverPath + 'usernavimage/' + filename)
    # 使用 send_file 函数发送图片文件
    return send_file(image_path, mimetype='image/jpeg')


@updata.route('/adminupimage', methods=['POST'])
def upload_admin_file():
    if 'file' not in request.files:
        return 'No file selected'
    file = request.files['file']
    if file.filename == '':
        return 'No file selected'
    if file:
        file.save(os.path.join(serverPath, 'adminnavimage/', file.filename))
        return domain + "/usernavimage/" + file.filename


@updata.route('/adminnavimage/<filename>')
def get_admin_image(filename):
    # 构建图片文件的完整路径
    image_path = os.path.join(serverPath + 'adminnavimage/' + filename)
    # 使用 send_file 函数发送图片文件
    return send_file(image_path, mimetype='image/jpeg')
